<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户列表</title>
    <base href="${pageContext.request.contextPath}/">
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<div style="padding: 15px;">
    <blockquote class="layui-elem-quote layui-quote-nm">
            <span class="layui-breadcrumb">
              <a href="">首页</a>
              <a><cite>用户管理</cite></a>
            </span>
    </blockquote>
    <div class="layui-form">
        <div class="layui-form-item user-query">
            <div class="layui-inline">
                <label class="layui-form-label" for="username">用户名</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="username" id="username" placeholder="请输入用户名称" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" for="name">员工姓名</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="name" id="name" placeholder="请输入员工姓名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" for="gender">性别</label>
                <div class="layui-input-inline">
                    <select name="gender" id="gender" lay-filter="user-query-select">
                        <option value="">请选择</option>
                        <option value="0">男</option>
                        <option value="1">女</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" for="departmentId">部门</label>
                <div class="layui-input-inline">
                    <select name="departmentId" id="departmentId" lay-search lay-filter="user-query-select">
                        <option value="">请选择</option>
                        <c:forEach items="${depts}" var="dept">
                            <option value="${dept.id}">${deptName}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">部门多选</label>
                <div class="layui-input-inline xm-select-demo" id="depts">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" for="birthdayBegin">生日</label>
                <div class="layui-input-inline" style="width: 120px;">
                    <input type="text" name="birthdayBegin" id="birthdayBegin" placeholder="" autocomplete="off"
                           class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 120px;">
                    <input type="text" name="birthdayEnd" id="birthdayEnd" placeholder="" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" for="mobile">电话号码</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="mobile" id="mobile" placeholder="电话" autocomplete="off"
                           class="layui-input">
                </div>
            </div>


            <div class="layui-inline">
                <button class="layui-btn" id="user-query" data-type="query">查询</button>
            </div>

        </div>
    </div>

    <table class="layui-hide" id="user-table" lay-filter="user-table"></table>
</div>

<!-- 表头工具栏 -->
<script type="text/html" id="thead-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增用户</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDelete">批量删除</button>
    </div>
</script>

<!-- 单元格工具栏 -->
<script type="text/html" id="row-toolbar">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm" lay-event="resetPassword">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>

<script src="layui/layui.js"></script>
<script src="js/xm-select-1.js"></script>
<script>
    layui.use(['element', 'table', 'form', 'layer', 'laydate'], function () {
        var element = layui.element,
            table = layui.table,
            form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            laydate = layui.laydate;


        // 渲染多选表单框
        var deptSelect = xmSelect.render({
            el: '#depts',
            data: []
        });
        //动态加载表单框数据
        <%--$.ajax({--%>
        <%--   url: '${pageContext.request.contextPath}/admin/',--%>
        <%--    --%>
        <%--});--%>

        // axios({
        //     method: 'get',
        //     url: 'https://www.fastmock.site/mock/98228b1f16b7e5112d6c0c87921eabc1/xmSelect/search',
        // }).then(response => {
        //     var res = response.data;
        //
        //     demo2.update({
        //         data: res.data,
        //         autoRow: true,
        //     })
        // });


        // 绑定日期框
        laydate.render({
            elem: "#birthdayBegin",
            max: $("#birthdayEnd").val()
        });
        laydate.render({
            elem: "#birthdayEnd",
            min: $("#birthdayBegin").val()
        })

        var tableIns = table.render({
            elem: '#user-table'
            , id: 'userId'
            , method: 'POST'
            , url: '${pageContext.request.contextPath}/admin/user/list'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                {type: 'checkbox'},
                {field: 'id', title: 'ID', sort: true},
                {field: 'name', title: '姓名'},
                {field: 'username', title: '用户名'},
                {field: 'department.name', title: '部门'},
                {
                    field: 'gender', title: '性别', templet: function (d) {
                        return d.gender == 0 ? '男' : '女'
                    }
                },
                {field: 'birthday', title: '生日'},
                {field: 'mobile', title: '电话号码'},
                {fixed: 'right', align: 'center', title: '编辑', toolbar: '#row-toolbar'}
            ]]
            , page: {
                limits: [10, 20]
            }
            , even: true
            , toolbar: '#thead-toolbar'
            , defaultToolbar: ['exports', 'print']
        });

        //监听表格复选框选择
        table.on('checkbox(user-table)', function (obj) {
            //
        });
        //监听行工具条
        table.on('tool(user-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除' + data.name + '么？', function (index) {
                    // 异步删除用户方法
                    $.ajax({
                        url: "${pageContext.request.contextPath}/admin/user/detail?id=" + data.id,
                        method: "DELETE",
                        success: function (data) {
                            if (data.msg == "true") {
                                layer.msg("用户删除成功！");
                                // 重载表格
                                $("#user-query").click();
                            } else {
                                layer.msg("用户删除失败！");
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    skin: 'layui-layer-molv',
                    area: ['400px', '200px'],
                    fixed: true, //不固定
                    title: '修改用户',
                    maxmin: true,
                    content: 'admin/user/detail?id=' + data.id
                });
            }
        });

        //头工具栏事件
        table.on('toolbar(user-table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchDelete':
                    var users = checkStatus.data;
                    $.ajax({
                        url: "${pageContext.request.contextPath}/admin/user/detail?ids=" + users.map(item => item.id).join(","),
                        method: "DELETE",
                        success: function (data) {
                            if (data.msg == "true") {
                                layer.msg("用户" + users.map(item => item.name) + "删除成功！");
                                // 重载表格
                                $("#user-query").click();
                            } else {
                                layer.msg("用户删除失败！");
                            }
                        }
                    });
                    break;
                case 'add':
                    layer.open({
                        type: 2,
                        skin: 'layui-layer-molv',
                        area: ['400px', '200px'],
                        fixed: true, //不固定
                        title: '新增用户',
                        maxmin: true,
                        content: 'admin/user/detail'
                    });
                    break;
            }
        });

        var active = {
            query: function () { //搜索表单
                tableIns.reload({
                    where: { //设定异步数据接口的额外参数，任意设
                        'name': $("#name").val(),
                        'username': $("#username").val(),
                        'gender': $("#gender").val(),
                        'birthdayBegin': $("#birthdayBegin").val(),
                        'birthdayEnd': $("#birthdayEnd").val(),
                        'departmentId': $("#departmentId").val(),
                        'mobile': $("#mobile").val()
                    }
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }
        };

        $('.user-query .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
</body>
</html>
